﻿using Lmondeil.Crm.EF;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Atlanchim.Crm.Mui.Pages.Alertes
{
    public class PageListAlertesModel : AbstractPageListModel
    {
        /// <summary>
        /// Liste des contacts
        /// </summary>
        public List<Alerte> AlertesList { get; set; }

        /// <summary>
        /// Chargement de la liste des contacts
        /// </summary>
        public override void Load()
        {
            // Récupération dun mode d'affichage
            string strMode = string.Empty;
            if (this.DicFragments != null)
            {
                if (this.DicFragments.ContainsKey("Mode"))
                {
                    strMode = this.DicFragments["Mode"];
                }
            }

            // Chargement des données
            if (strMode.ToLower() == "day")
            {
                LoadDay();
            }
            else if (strMode.ToLower() == "week")
            {
                LoadWeek();
            }
            else
            {
                LoadDefault();
            }

            // Url pour la création d'une nouvelle alerte
            this.NewEntityUrl = "/Pages/Alertes/PageEditAlerte.xaml#IdAlerte=0";
        }

        /// <summary>
        /// Chargement de toutes les alertes
        /// </summary>
        private void LoadDefault()
        {
            using (CrmContext context = new CrmContext())
            {
                //Chargement des sociétés
                this.AlertesList = context
                                    .Alertes
                                    .Include("Contact")
                                    .OrderBy(alerte => alerte.Date)
                                    .ToList();
            }
        }

        /// <summary>
        /// Chargement des alertes du jour
        /// </summary>
        private void LoadDay()
        {
            using (CrmContext context = new CrmContext())
            {
                // Date du jour
                DateTime todayDate = DateTime.Today;

                //Chargement des alertes
                this.AlertesList = context
                                    .Alertes
                                    .Include("Contact")
                                    .Where(alerte => alerte.Date == todayDate)
                                    .OrderBy(alerte => alerte.Date)
                                    .ToList();
            }
        }

        /// <summary>
        ///  Chargement des alertes de la semaine
        /// </summary>
        private void LoadWeek()
        {
            int thisDayOfWeek = (int)DateTime.Today.DayOfWeek;
            DateTime thisMonday = DateTime.Today.AddDays(thisDayOfWeek - 1);
            DateTime thisFriday = DateTime.Today.AddDays(thisDayOfWeek - 1 + 5);

            using (CrmContext context = new CrmContext())
            {
                //Chargement des alertes
                this.AlertesList = context
                                    .Alertes
                                    .Include("Contact")
                                    .Where(alerte => (thisMonday <= alerte.Date) && (alerte.Date <= thisFriday))
                                    .OrderBy(alerte => alerte.Date)
                                    .ToList();
            }
        }
    }
}